<?php

defined('SYSPATH') or die('Access error!');

class Cache
{

    private static $instances;

    public static function instance()
    {
        if (!isset(Cache::$instances))
        {
            Cache::$instances = new Cache();
        }
        return Cache::$instances;
    }

    /**
     * 读结果缓存文件
     * @staticvar array $result
     * @param type $filename
     * @return array|boolean
     */
    function read($filename)
    {
        static $result = array();
        if (!empty($result[$filename]))
        {
            return $result[$filename];
        }
        if (file_exists($filename))
        {
            include_once($filename);
            $result[$filename] = $data;
            return $result[$filename];
        }
        else
        {
            return false;
        }
    }

    /**
     * 写结果缓存文件
     * @param type $filename 
     * @param type $data
     * @return type
     */
    function write($filename, $data)
    {
        $content = "<?php\r\n";
        $content .= "\$data = " . var_export($data, true) . ";\r\n";
        $content .= "?>";
        return file_put_contents($filename, $content, LOCK_EX);
    }

}